package org.edu.jgame.cardgames;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class OptionMenu extends BasicGameState {

	int stateID = -1;
	
	Image background = null;
	Image changeButton = null;
	Image backButton = null;
	Image markBack = null;
	Image[] backCard = null;
	
	float changex = 0;
	float changey = 0;
	float backx = 0;
	float backy = 0;
	float markx = 0;
	float marky = 0;
	float cardx = 0;
	float cardy = 0;
	
	boolean backpress = false;
	boolean changepress = false;
	int idx_card = 0;
	
	public OptionMenu(int id) {		
		stateID = id;
	}
	
	@Override
	public void init(GameContainer arg0, StateBasedGame arg1)
			throws SlickException {
		background = new Image("data/OptionMenu.png");
		changeButton = new Image("data/ChangeButton.png");
		backButton = new Image("data/BackButton.png");
		markBack = new Image("data/MarkBack.png");
		backCard = new Image[2];
		backCard[0] = new Image("data/b1fv.png");
		backCard[1] = new Image("data/b2fv.png");
		
		changex = 725;
		changey = 600 - 471;
		backx = 65;
		backy = 448;
		markx = 321;
		marky = 249;
		cardx = 325;
		cardy = 254;
	}

	@Override
	public void render(GameContainer arg0, StateBasedGame arg1, Graphics arg2)
			throws SlickException {
		background.draw();
		changeButton.draw(changex, changey);
		if(backpress) {
			backButton.draw(backx, backy, Color.white);
		}
		else {
			backButton.draw(backx, backy);
		}
		markBack.draw(markx, marky);
		backCard[0].draw(cardx, cardy);
		backCard[1].draw(cardx+126, cardy);
		
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int delta)
			throws SlickException {
		Input input = gc.getInput();
		
		if((input.getMouseX() >= changex && input.getMouseX() <= changex + changeButton.getWidth()) 
				&& (input.getMouseY() >= changey && input.getMouseY() <= changey + changeButton.getHeight())) {
			if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
				changepress = true;				
			}
			else {
				changepress = false;
			}
		}
		
		if(changepress) {
			changex = input.getMouseX() - changeButton.getWidth()/2;
			if(changex <= 263) {
				changex = 263;
			}
			if(changex >= 725) {
				changex = 725;
			}
		}
		

		if((input.getMouseX() >=cardx && input.getMouseX() <= cardx + backCard[0].getWidth()) 
				&& (input.getMouseY() >= cardy && input.getMouseY() <= cardy + backCard[0].getHeight())) {
			if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
				markx = 321;
				idx_card = 0;
			}
		}
		
		if((input.getMouseX() >=cardx + 126 && input.getMouseX() <= cardx + 126 + backCard[1].getWidth()) 
				&& (input.getMouseY() >= cardy && input.getMouseY() <= cardy + backCard[1].getHeight())) {
			if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
				markx = 321 + 126;
				idx_card = 1;
			}
		}
		
		
		if((input.getMouseX() >= backx && input.getMouseX() <= backx + backButton.getWidth()) 
				&& (input.getMouseY() >= backy && input.getMouseY() <= backy + backButton.getHeight())) {
			if(input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
				try  {
					File f = new File("data/config");
					f.createNewFile();
					FileOutputStream fos = new FileOutputStream("data/config");
					OutputStreamWriter out = new OutputStreamWriter(fos);
					out.write("Volume=" + (changex - 263)/462 + "\n");
					if(idx_card == 0) {
						out.write("back= data/b1fv.png");
					}
					else {
						out.write("back= data/b2fv.png");
					}
					out.close();
					fos.close();
				}
				catch(FileNotFoundException e) {
					
				}
				catch(IOException e) {
					
				}
				sbg.enterState(JCardGame.MAINMENUSTATE);			
			}
		}
		
	}

	@Override
	public int getID() {
		// TODO Auto-generated method stub
		return stateID;
	}

}
